package com.ksd.modules.course.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.course.po.AdminCourseChapterSavePO;
import com.ksd.modules.course.service.IAdminCourseChapterService;
import com.ksd.modules.course.vo.AdminCourseChapterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AdminCourseChapterController extends BaseController {
    
    @Autowired
    private IAdminCourseChapterService adminCourseChapterService;


    /**
     * 【课程】-【章列表】
     * @return
     */
    @PostMapping("/course/chapter/list")
    public R<List<AdminCourseChapterVo>> findAdminCourseChapters(@RequestParam("courseId")Long courseId){
        return R.data(adminCourseChapterService.findAdminCourseChapters(courseId));
    }

    /**
     * 【课程】-【章】-保存和更新
     * @param adminCourseChapterSavePO
     * @return
     */
    @PostMapping("/course/chapter/saveupdate")
    @ApiLog("【课程】-【章】-保存和更新")
    @RepeatSubmit
    public R<Boolean> saveUpdateAdminCourseChapter(@Validated @RequestBody AdminCourseChapterSavePO adminCourseChapterSavePO){
        return R.data(adminCourseChapterService.saveUpdateAdminCourseChapter(adminCourseChapterSavePO));
    }

    /**
     * 【课程】-【章】-根据id明细查询
     * @param id
     * @return
     */
    @PostMapping("/course/chapter/get/{id}")
    public R<AdminCourseChapterVo> getAdminCourseChapterById(@PathVariable("id")Long id){
        return R.data(adminCourseChapterService.getAdminCourseChapterById(id));
    }

    /**
     * 【课程】-【章】- 根据ID删除
     * @param id
     * @return
     */
    @PostMapping("/course/chapter/remove/{id}")
    @ApiLog("【课程】-【章】- 根据ID删除")
    public R<Boolean> removeAdminCourseChapterById(@PathVariable("id")Long id){
        return R.data(adminCourseChapterService.removeAdminCourseChapterById(id));
    }

    /**
     *【课程】-【章】- 修改状态
     * @return
     */
    @PostMapping("/course/chapter/status/{id}/{field}")
    @ApiLog("课程】-【章】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminCourseChapterService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminCourseChapterService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【课程分类】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/course/chapter/copy/{opid}")
    @ApiLog("【课程分类】- 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminCourseChapterService.copyData(opid));
    }
}
